package by.amakarevich.webapp.page.edit.kit;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.amakarevich.datamodel.Kit;
import by.amakarevich.datamodel.Product;
import by.amakarevich.services.KitService;
import by.amakarevich.webapp.page.AlternativeBaseLayout;
import by.amakarevich.webapp.page.home.HomePage;


public class KitCreatePage extends AlternativeBaseLayout {

	@Inject
	private KitService kitService;
	
	public KitCreatePage(final Kit kit, final Product product) {
		super();
		Form<Kit> form = new Form<Kit>("form", new CompoundPropertyModel<Kit>(kit));

		final TextField<String> tfName = new TextField<String>("name");
		tfName.add(new PropertyValidator<String>());
		tfName.setLabel(new ResourceModel("p.product.name"));
		form.add(tfName);

		final TextField<Long> tfHeight = new TextField<Long>("height");
		tfHeight.add(new PropertyValidator<Long>());
		tfHeight.setLabel(new ResourceModel("p.product.height"));
		form.add(tfHeight);
		
		final TextField<Long> tfWidth = new TextField<Long>("width");
		tfWidth.add(new PropertyValidator<Long>());
		tfWidth.setLabel(new ResourceModel("p.product.width"));
		form.add(tfWidth);
		
		final TextField<Long> tfCanvas = new TextField<Long>("canvas");
		tfCanvas.add(new PropertyValidator<Long>());
		tfCanvas.setLabel(new ResourceModel("p.product.canvas"));
		form.add(tfCanvas);
		
		final TextField<String> tfKitColor = new TextField<String>("kitColor");
		tfKitColor.add(new PropertyValidator<String>());
		tfKitColor.setLabel(new ResourceModel("p.product.kit.color"));
		form.add(tfKitColor);
		
		final TextArea<String> tfSpecification = new TextArea<String>("specification");
		tfSpecification.add(new PropertyValidator<String>());
		tfSpecification.setLabel(new ResourceModel("p.product.specification"));
		form.add(tfSpecification);

		form.add(new SubmitLink("sumbit-link") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				if(kit.getId()==null){
					kitService.createNewKit(kit, product);
				}else{
					kitService.updateKit(kit);
				}
					
				HomePage page = new HomePage();


				setResponsePage(page);
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		add(form);

	}
		

}
